local skel = fk.CreateSkill {
  name = "emo__maochai",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__maochai"] = "猫柴",
  [":emo__maochai"] = "锁定技，其他角色每回合首次改变体力上限时，你令任意名本轮体力未上限改变过的角色本回合所有技能失效，然后回合结束时你观看该角色的手牌，并为其选择一项：1.对你发动一次“结芬”；2.将其至多三张手牌扣置在你的武将牌上，你下回合开始时，你获得这些牌。",

  ["#emo__maochai-invalidate"] = "猫柴：令任意名本轮体力未上限改变过的角色本回合所有技能失效",
  ["@@emo__maochai-turn"] = "猫柴:封印",
  ["#emo__maochai-choice"] = "猫柴：为 %src 选择一项",
  ["emo__maochai_marry"] = "发动“结芬”",
  ["emo__maochai_put"] = "扣置三张牌",
}

skel:addEffect(fk.MaxHpChanged, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target ~= player then
      local currentEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.ChangeMaxHp, true)
      if currentEvent == nil then return false end
      local first = player.room.logic:getEventsOfScope(GameEvent.ChangeMaxHp, 1, function (e)
        return e.data.who == target
      end, Player.HistoryTurn)[1]
      return first == currentEvent
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "emo__maochai-turn", target.id)
    local changed = {}
    room.logic:getEventsOfScope(GameEvent.ChangeMaxHp, 1, function (e)
      table.insertIfNeed(changed, e.data.who)
    end, Player.HistoryRound)
    local targets = table.filter(room.alive_players, function (p)
      return not table.contains(changed, p)
    end)
    if #targets > 0 then
      local tos = room:askToChoosePlayers(player, {
        targets = targets, min_num = 1, max_num = 99, skill_name = skel.name, prompt = "#emo__maochai-invalidate"
      })
      for _, p in ipairs(tos) do
        room:setPlayerMark(p, "@@emo__maochai-turn", 1)
      end
    end
  end,
})

skel:addEffect("invalidity", {
  invalidity_func = function(self, player, skill)
    return player:getMark("@@emo__maochai-turn") ~= 0 and not (skill.name:endsWith("&"))
  end,
})

skel:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return player:getMark("emo__maochai-turn") ~= 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = table.map(player:getTableMark("emo__maochai-turn"), Util.Id2PlayerMapper )---@type ServerPlayer[]
    room:sortByAction(tos)
    for _, to in ipairs(tos) do
      if player.dead then break end
      if not to.dead and not to:isKongcheng() then
        local hand = to:getCardIds("h")
        local cards, choice = room:askToChooseCardsAndChoice(player, {
          cards = hand, choices = {"emo__maochai_marry", "emo__maochai_put"}, skill_name = skel.name,
          min_num = 1, max_num = 3, cancel_choices = {"Cancel"}, prompt = "#emo__maochai-choice:"..to.id,
          --filter_skel_name = skel.name,
        })
        if choice and choice ~= "Cancel" and #cards > 0 then
          if choice == "emo__maochai_marry" then
            local marry = Fk.skills["emo__jiefen_other&"]
            if marry and marry:isInstanceOf(ActiveSkill) then
              ---@cast marry ActiveSkill
              marry:onUse(room, SkillUseData:new {
                from = to, tos = {player}, cards = {cards[1]}
              })
            end
          else
            player:addToPile(skel.name, cards, false, skel.name)
          end
        end
      end
    end
  end,
})

--- 回合开始时，获得武将牌上的牌
skel:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and #player:getPile(skel.name) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:obtainCard(player, player:getPile(skel.name), false, fk.ReasonPrey, player, skel.name)
  end,
})

--[[
skel.extra = {
  choiceFilter = function (cards, choice, extra_data)
    if choice == "emo__maochai_put" then
      return #cards > 0 and #cards < 4
    elseif choice == "emo__maochai_marry" then
      return #cards == 1
    end
    return false
  end
}
--]]

return skel
